// 版权归 瞄你个汪 所有。使用本代码应遵守相关法律法规和Apache 2.0开源许可要求。
package top.geeke.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.geeke.config.SysUserSessionManager;
import top.geeke.entity.SysUser;

/**
 * <p>
 * 系统用户表 DAO接口
 * </p>
 * @author 瞄你个汪
 * @since 2024-05-01 12:00:00
 */
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {
    @Select("SELECT u.id, u.account, u.real_name, u.account_type, u.tenant_id, u.org_id, u.manager_id, u.pos_id, t.app_id, o.org_name, o.org_type " +
            "FROM sys_user u " +
            "INNER JOIN sys_tenant t ON u.tenant_id = t.id " +
            "LEFT JOIN sys_org o ON u.org_id = o.id " +
            "WHERE u.id = #{userId}")
    SysUserSessionManager.SysUserSession getSysUserSession(@Param("userId")Long userId);

    /**
     * 根据 账号名或手机号 + 租户id 查询用户
     */
    @InterceptorIgnore(tenantLine = "true")
    default SysUser getUserByAccountAndTenant(String account, Long tenantId) {
        return selectOne(new QueryWrapper<SysUser>().lambda()
                .eq(SysUser::getTenantId, tenantId)
                .and(q -> q.eq(SysUser::getAccount, account).or()
                        .eq(SysUser::getMobile, account)));
    }
}
